using System;
using System.Data;
using System.Web.UI.WebControls;
using NCS.Library;
using NCS.Library.Components.Partner;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Zones;

namespace NCS.Module.CMS.Web.News
{
    public partial class RightContentBelowAds1 : CMSWebControl
    {
        public int zoneid;
        protected void Page_Load(object sender, EventArgs e)
        {
            int catid = ConvertUtility.ToInt32(Request.QueryString["catid"]);
            if (catid != 0)
            {
                Advertisement1.Param = "catid";
            }
            
            if(curZone != null)
            {
                zoneid = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + curZone.Zone_ID + "_" + AppEnv.GetLanguage()]);
            }
            if(zoneid == 0)
                zoneid = ConvertUtility.ToInt32(Portal[Constants.CMS_TinNoiBat + AppEnv.GetLanguage()]);

            DataTable right = DistributionController.GetDistributedRightContents(zoneid, 3, true);
            ZoneInfo info = ZoneController.GetZone(zoneid);
            if (info != null)
            {
                hplCaption.Text = info.Zone_Name;
                hplCaption.NavigateUrl = BuildUrlByZone(zoneid, info.Portal_TabControlID);
                hplMore.NavigateUrl = BuildUrlByZone(zoneid, info.Portal_TabControlID);
            }
            rptRightContentBelowAds.DataSource = right;
            rptRightContentBelowAds.ItemDataBound += new RepeaterItemEventHandler(rptRightContentBelowAds_ItemDataBound);
            rptRightContentBelowAds.DataBind();

        }

        void rptRightContentBelowAds_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                Image imgIcon = (Image)e.Item.FindControl("imgIcon");
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                Literal litSummary = (Literal)e.Item.FindControl("litSummary");

                imgIcon.ImageUrl = MultimediaUtility.GetListAvatar(ConvertUtility.ToString(currow["Content_Avatar"]));
                imgIcon.Attributes.Add("onclick", "javascript:window.location='" + BuildUrlByDistId(ConvertUtility.ToInt32(currow["Distribution_ID"]), zoneid) + "'");
                imgIcon.Attributes.Add("style", "cursor:hand");
                
                hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

                string tooltip = MiscUtility.ToolTip(ConvertUtility.ToString(currow["Content_Headline"]), ConvertUtility.ToString(currow["Content_SubTitle"]), ConvertUtility.ToString(currow["Content_Avatar"]), ConvertUtility.ToString(currow["Content_Teaser"]), ConvertUtility.ToString(currow["Content_Alias"]), ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                hplTitle.Attributes.Add("onmouseover", "showtip('" + tooltip + "','322','#EBF3FB')");
                hplTitle.Attributes.Add("onmouseout", "hidetip()");

                hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], zoneid);
                litSummary.Text = ConvertUtility.ToStringWithoutFontBoldLength(currow["Content_Teaser"], ConvertUtility.ToString(currow["Content_Headline"]).TrimEnd().Length);
            }
        }
    }
}